﻿/****************************** Module Header ******************************\
* Plus Name:  CNRegion.js
* Plus Version:  1.0.0 (2012-1-9)
* Copyright (c) JianDanMai.Com (14507247@qq.com)
* 
* This plus used to option the china region webcontrols.
* 
* This source is subject to the GPL License.
* See http://www.gnu.org/licenses/gpl.html.
* All other rights reserved.
* 
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

/*
* ==Arguments:==
*   cnRegionXML     :   省市区XML压缩数据
*   provinceID      :   省份select控件ID
*   provinceValue   :   省份初始值
*   provinceHiddenID:   ASP.NET HiddenField服务器控件的编号
*   isReadValueField:   值是下拉控件中的value字段还是text字段,true为value字段
* ==Demo:==
*   SetCNRegion('<%=CnRegionXML %>', "#province", "#city", "#area", "福建省", "厦门市", "思明区", "#provinceHidden", "#cityHidden", "#areaHidden");
*   SetCNRegion('<?xml version="1.0" encoding="utf-8" ?><cnregion><province code="110000" name="北京市"><city code="110100" name="市辖区"><area code="110101" name="东城区" /></city></province></cnregion>', "#province", "#city", "#area", "福建省", "厦门市", "思明区", "#provinceHidden", "#cityHidden", "#areaHidden",true);
*   <select id="province"><option value="">请选择省份</option></select>
*   <select id="city"><option value="">请选择城市</option></select>
*   <select id="area"><option value="">请选择地区</option></select>
*   <asp:HiddenField runat="server" ID="provinceHidden" />
*   <asp:HiddenField runat="server" ID="cityHidden" />
*   <asp:HiddenField runat="server" ID="areaHidden" />
*/


SetCNRegion = function(cnRegionXML, provinceID, cityID, areaID, provinceValue, cityValue, areaValue, provinceHiddenID, cityHiddenID, areaHiddenID, isReadValueField) {
    if (!cnregion) var cnregion = {};
    // Read the XML data of chinese region from server script
    cnregion.$xmlCnRegion = $($.parseXML(cnRegionXML));
    cnregion.$provinceSelect = $(provinceID);
    cnregion.$citySelect = $(cityID);
    cnregion.$areaSelect = $(areaID);
    cnregion.provinceValue = provinceValue;
    cnregion.cityValue = cityValue;
    cnregion.areaValue = areaValue;
    cnregion.$provinceHidden = $(provinceHiddenID);
    cnregion.$cityHidden = $(cityHiddenID);
    cnregion.$areaHidden = $(areaHiddenID);
    cnregion.valueFieldName = ((isReadValueField == true) ? "code" : "name");
    // Initialize city list
    cnregion.InitCity = function() {
        cnregion.$xmlCnRegion.find("province[" + cnregion.valueFieldName + "='" + cnregion.provinceValue + "']").children().each(function() { cnregion.$citySelect.append("<option value='" + $(this).attr(cnregion.valueFieldName) + "'>" + $(this).attr("name") + "</option>"); });
        cnregion.$provinceHidden.val(cnregion.provinceValue);
    };
    // Initialize area List
    cnregion.InitArea = function() {
        cnregion.$xmlCnRegion.find("city[" + cnregion.valueFieldName + "='" + cnregion.cityValue + "']").children().each(function() { cnregion.$areaSelect.append("<option value='" + $(this).attr(cnregion.valueFieldName) + "'>" + $(this).attr("name") + "</option>"); });
        cnregion.$cityHidden.val(cnregion.cityValue);
    };
    // Initialize province list
    cnregion.$xmlCnRegion.find("province").each(function() { cnregion.$provinceSelect.append("<option value='" + $(this).attr(cnregion.valueFieldName) + "'>" + $(this).attr("name") + "</option>"); });
    // Initialize select controls
    if (cnregion.provinceValue) {
        cnregion.$provinceSelect.children("option[value='" + cnregion.provinceValue + "']").attr("selected", "selected");
        cnregion.InitCity();
    }
    if (cnregion.cityValue) {
        cnregion.$citySelect.children("option[value='" + cnregion.cityValue + "']").attr("selected", "selected");
        cnregion.InitArea();
    }
    if (cnregion.areaValue) {
        cnregion.$areaSelect.children("option[value='" + cnregion.areaValue + "']").attr("selected", "selected");
        cnregion.$areaHidden.val(cnregion.areaValue);
    }

    // Add the event for the controls
    cnregion.$provinceSelect.change(function() {
        cnregion.$citySelect.html("<option value=''>请选择城市</option>");
        cnregion.$areaSelect.html("<option value=''>请选择地区</option>");
        cnregion.provinceValue = cnregion.$provinceSelect.val();
        if (!cnregion.provinceValue) return;
        cnregion.InitCity();

    });
    cnregion.$citySelect.change(function() {
        cnregion.$areaSelect.html("<option value=''>请选择地区</option>");
        cnregion.cityValue = cnregion.$citySelect.val();
        if (!cnregion.cityValue) return;
        cnregion.InitArea();
    });
    cnregion.$areaSelect.change(function() {
        cnregion.areaValue = cnregion.$areaSelect.val();
        cnregion.$areaHidden.val(cnregion.areaValue);
    });
};

    
